//
//  MYGFindDetailCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/12.
//

import UIKit
import FSPagerView
import QMUIKit

class MYGFindDetailCell: UITableViewCell {
    var clickImageBlock:(()->Void)?
    var model: MYGFindListModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            if model.medias.count < 2 {
                pagerView.isInfinite = false
            } else {
                pagerView.isInfinite = true
            }
            pagerView.reloadData()
            nameLabel.text = model.content
            timeLabel.text = "\(model.created_at) \(model.address)"
        }
    }
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.addSubview(pagerView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(backView)
        
        pagerView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(200)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(12)
            make.top.equalTo(pagerView.snp.bottom).offset(15)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.top.equalTo(nameLabel.snp.bottom).offset(15)
            make.bottom.equalToSuperview().inset(15)
        }
        backView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    // MARK: - lazy load
    private lazy var backView: QMUIButton = {
        let view = QMUIButton()
        view.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return view
    }()
    private(set) lazy var pagerView: FSPagerView = {
        let pager = FSPagerView()
        pager.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
        pager.delegate = self
        pager.dataSource = self
        pager.automaticSlidingInterval = 3
        pager.isInfinite = true
        return pager
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(33, 33, 33, 1)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(118, 118, 118, 1)
        label.text = "12-03 北京市"
        return label
    }()
    
    @objc func clickButton() {
        if model!.type == 2 {
            return
        }
        
        clickImageBlock?()
    }
}

extension MYGFindDetailCell: FSPagerViewDelegate, FSPagerViewDataSource {
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        model?.medias.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        cell.imageView?.sd_setImage(with: URL(string: model?.medias[index] ?? ""))
        return cell
    }
}
